package com.atguigu.day05;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

public class Flink07_WindowFun_Agg {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读数据并转为WaterSensor
        KeyedStream<WaterSensor, String> keyedStream = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                })
//                .keyBy(WaterSensor::getId)
//                .keyBy(r->r.getId())
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                });

        // 3.开启一个基于时间的 滚动窗口 开启窗口大小为5S的滚动窗口
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

        //TODO 使用Agg实现Sum的功能，对窗口中的vc做累加
        window.aggregate(new AggregateFunction<WaterSensor, Integer, Integer>() {
            /**
             * 创建累加器
             *
             * @return
             */
            @Override
            public Integer createAccumulator() {
                System.out.println("创建累加器。。。。");
                return 0;
            }

            /**
             * 累加操作
             *
             * @param value
             * @param accumulator
             * @return
             */
            @Override
            public Integer add(WaterSensor value, Integer accumulator) {
                System.out.println("累加操作");
                return value.getVc() + accumulator;
            }

            /**
             * 获取结果
             *
             * @param accumulator
             * @return
             */
            @Override
            public Integer getResult(Integer accumulator) {
                System.out.println("获取结果");
                return accumulator;
            }

            /**
             * 合并累加器  只有在会话窗口特殊的情况下才会调用其他窗口不调用
             *
             * @param a
             * @param b
             * @return
             */
            @Override
            public Integer merge(Integer a, Integer b) {
                System.out.println("合并累加器。。。");
                return a + b;
            }
        }).print();

        env.execute();

    }
}
